import java.rmi.RemoteException;
import java.security.spec.AlgorithmParameterSpec;

import javax.xml.rpc.ServiceException;

import org.apache.ws.axis2.TestMobileSignService;
import org.apache.ws.axis2.TestMobileSignServiceLocator;
import org.apache.ws.axis2.TestMobileSignServicePortType;

import tr.gov.tubitak.uekae.esya.api.asn.x509.ECertificate;
import tr.gov.tubitak.uekae.esya.api.common.ESYAException;
import tr.gov.tubitak.uekae.esya.api.common.util.Base64;
import tr.gov.tubitak.uekae.esya.api.infra.mobile.MSSPClientConnector;
import tr.gov.tubitak.uekae.esya.api.infra.mobile.Operator;
import tr.gov.tubitak.uekae.esya.api.infra.mobile.PhoneNumberAndOperator;
import tr.gov.tubitak.uekae.esya.api.infra.mobile.SigningMode;
import tr.gov.tubitak.uekae.esya.api.infra.mobile.UserIdentifier;

public class EMSSPClientConnector implements MSSPClientConnector
{
    public ECertificate[] getCertificates(UserIdentifier aUserID)
    {
         PhoneNumberAndOperator phoneNumberAndOperator = (PhoneNumberAndOperator) aUserID;
         TestMobileSignServiceLocator locator = new TestMobileSignServiceLocator();
     	TestMobileSignServicePortType stub = null;
     	try {
     		stub = locator.getTestMobileSignServiceHttpSoap11Endpoint();
     		String certValueBase64= stub.getUserCertificate("05380892868",0);
         	ECertificate userCert = null;
         	userCert = new ECertificate(Base64.decode(certValueBase64));         	
         	ECertificate[] cert = new ECertificate[1];
         	cert[0]=userCert;
         	return cert;
     	} catch (Exception e1) {
     		// TODO Auto-generated catch block
     		e1.printStackTrace();
     		return null;
     	}     	
     	
     	
    }

        public byte[] sign(byte[] dataToBeSigned, SigningMode aMode, UserIdentifier aUserID, ECertificate aSigningCert, String informativeText, String aSigningAlg,AlgorithmParameterSpec algSpec)
        {
            if(aMode!=SigningMode.SIGNHASH)
            {
                try {
					throw new Exception("Unsuported signing mode. Only SIGNHASH supported.");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
            }

            PhoneNumberAndOperator phoneNumberAndOperator = (PhoneNumberAndOperator) aUserID;
            TestMobileSignServiceLocator locator = new TestMobileSignServiceLocator();
        	TestMobileSignServicePortType stub = null;
        	try {
        		stub = locator.getTestMobileSignServiceHttpSoap11Endpoint();
        	} catch (ServiceException e1) {
        		// TODO Auto-generated catch block
        		e1.printStackTrace();
        	}     	
        	
        	String dataTobeSigned64 = Base64.encode(dataToBeSigned);            
        	Operator opes= phoneNumberAndOperator.getOperator();        	
            String signatureBase64;
			try {
				signatureBase64 = stub.signHash(dataTobeSigned64, informativeText, phoneNumberAndOperator.getPhoneNumber(),opes.ordinal());
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
           if(signatureBase64!=null)
           {
        	   return Base64.decode(signatureBase64);               
           }
            return null;
        }
		
}
